package com.qvtu.management.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qvtu.management.mapper.EmpMapper;
import com.qvtu.management.pojo.Emp;
import com.qvtu.management.pojo.PageBean;
import com.qvtu.management.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    //分页查询
    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender,
                        LocalDate begin,
                         LocalDate end) {
        // 计算 start 参数
        Integer start = (page - 1) * pageSize;

        PageHelper.startPage(page, pageSize);
        // 执行分页查询

        List<Emp> empList = empMapper.list(start, pageSize, name, gender, begin, end);

        System.out.println(page + ' ' + pageSize + name + gender + begin + end);

        PageInfo<Emp> pageInfo = new PageInfo<>(empList);
        Long countWithConditions = empMapper.countWithConditions(name, gender, begin, end);
        // 根据条件重新计算总记录数

        PageBean pageBean = new PageBean(countWithConditions, pageInfo.getList());


        return pageBean;
    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        //可能没有加入时间
        empMapper.insert(emp);
    }

    //根据id查询
    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }
    //更新员工信息
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }
    //@Override
//public PageBean page(Integer page, Integer pageSize) {
//    // 1. 设置分页参数
//    PageHelper.startPage(page, pageSize);
//    // 2. 执行查询
//    List<Emp> empList = empMapper.list();
//    PageInfo<Emp> pageInfo = new PageInfo<>(empList);
//    // 创建 PageBean 对象
//    PageBean pageBean = new PageBean(pageInfo.getTotal(), pageInfo.getList());
//
//    return pageBean;
//}

}
